package 
{
	import b2.net.*;
	import b2.player.*;
	import b2.scene.*;
	import b2.scene.base.*;
	import b2.scene.guide.*;
	import b2.util.*;
	import b2.widget.*;
	import b2.widget.warehouse.*;
	import b2.widget.task.*;
	import b2.widget.login.*;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.utils.*;

	[SWF(backgroundColor="#AAAAAA", frameRate="60", quality="MEDIUM", width="760", height="600")]
	public class B2 extends Sprite
	{
		public function B2()
		{
			//调试面板
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			//读入参数
			var param:Object = loaderInfo.parameters;
			DebugPane.getInstance().addInfo("客户端版本: " + Profile.VERSION);
			//本地调试用
			if (Profile.DEBUG == 1)
			{
				PlayerInfoManager.getInstance().openid = "8";
				PlayerInfoManager.getInstance().openkey = "CCE83B952D4076BE7B3FCA955E5C3A65";
				PlayerInfoManager.getInstance().pf = "";
				PlayerInfoManager.getInstance().pfkey = "";
				startAll();
			}
			//远程调试用
			else if (Profile.DEBUG == 2)
			{
				PlayerInfoManager.getInstance().openid = "F0E3E4CE4A4A348BBC10007FF931E571";
				PlayerInfoManager.getInstance().openkey = "B384A87392E9368DDCC34F54FEBFDEE6";
				PlayerInfoManager.getInstance().pf = "pengyou";
				PlayerInfoManager.getInstance().pfkey = "d588fa9d58dfa5b5ed8730097522f220";
//				PlayerInfoManager.getInstance().openid = "AADF7EC02ED053FFD6E164BE9A977B9E";
//				PlayerInfoManager.getInstance().openkey = "4B4E5CC11DA884DC8165DC49E9F9351B";
//				PlayerInfoManager.getInstance().pf = "pengyou";
//				PlayerInfoManager.getInstance().pfkey = "9682b56d2ad8ead5d52592d94b059563";
				startAll();
			}
			//QQ用户
			else if (param.hasOwnProperty("openid") && param.hasOwnProperty("openkey"))
			{
				//QQ用户信息
				PlayerInfoManager.getInstance().openid = param["openid"];
				PlayerInfoManager.getInstance().openkey = param["openkey"];
				PlayerInfoManager.getInstance().pf = param["pf"];
				PlayerInfoManager.getInstance().pfkey = param["pfkey"];
				//开始进入
				startAll();
			}
			//尚未授权
			else
			{
				var authenMessage:AuthenMessage = new AuthenMessage();
				authenMessage.x = (BFont.stageWidth - authenMessage.width) / 2;
				authenMessage.y = (BFont.stageHeight - authenMessage.height) / 3;
				addChild(authenMessage);
			}
		}
		
		private function startAll():void
		{
			//调试信息
			DebugPane.getInstance().addInfo("openid:" + PlayerInfoManager.getInstance().openid);
			DebugPane.getInstance().addInfo("openkey:" + PlayerInfoManager.getInstance().openkey);
			DebugPane.getInstance().addInfo("pf:" + PlayerInfoManager.getInstance().pf);
			DebugPane.getInstance().addInfo("pfkey:" + PlayerInfoManager.getInstance().pfkey);
			Profile.setPlatform(PlayerInfoManager.getInstance().pf);
			//开始加载主资源
			var loadingScene:LoadingScene = new LoadingScene("正在加载主资源……");
			SceneManager.getInstance().loadScene(loadingScene);
			ResourceHelper.getInstance().startLoadMainResource(loadingScene.loadBar.updateProgress, main);
			addChild(SceneManager.getInstance());
		}
		
		public function onKeyDown(e:KeyboardEvent):void 
		{
			if (e.keyCode == 27)
			{
				if (DebugPane.getInstance().parent == null)
				{
					addChild(DebugPane.getInstance());
				}
				else
				{
					DebugPane.getInstance().close();
				}
			}
		}
		
		/**
		 * 加载完成后的主函数
		 */
		public function main():void
		{
			//主音乐
			BSound.getInstance().playMain();
			BSound.getInstance().musicVolume = 0;
			BSound.getInstance().effectVolume = 0;
			ServerHelper.getInstance().addEventListener(ServerEvent.LOGIN_ME, onLoginComplete);
			//调试版本
			if (Profile.DEBUG > 0)
			{
				ServerHelper.getInstance().loginMe();
			}
			//发布版本
			else
			{
				ServerHelper.getInstance().loginMe();
			}
		}
		
		private function onLoginComplete(event:ServerEvent):void
		{
			//加载主基地
			SceneManager.getInstance().loadScene(BaseScene.getInstance());
			var me:PlayerInfo = PlayerInfoManager.getInstance().me;
			Warehouse.getInstance().refresh();
			TaskDialog.getInstance().refresh();
			//加载新手引导
			if (!me.finishGuide)
			{
				var guideScene:GuideScene = GuideScene.getInstance();
				addChild(guideScene);
				guideScene.step = 0;
			}
			else if (!me.hasFetchedGift())
			{
				var loginDialog:LoginDialog = new LoginDialog(me.logonDayCount);
				WidgetManager.getInstance().popOut([loginDialog], true, true);
			}
			//失去焦点时候提示
			var focusLostScreen:FocusLostScreen = FocusLostScreen.getInstance();
			addChild(focusLostScreen);
			stage.addEventListener(Event.DEACTIVATE, onFocusLost);
			stage.addEventListener(Event.ACTIVATE, onFocus);
			focusLostScreen.addEventListener(MouseEvent.CLICK, onFocus);
		}
		
		private function onFocusLost(event:Event):void
		{
			FocusLostScreen.getInstance().enable = true;
		}

		private function onFocus(event:Event):void
		{
			FocusLostScreen.getInstance().enable = false;
		}
	}
}
